package com.dhu.wx.handler;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @Author gongjiawei
 * @Date 2025/3/5 12:24
 * @注释
 */
@Component
@Slf4j
public class SubscribeMsgHandler implements WxChatMsgHandler{
    @Override
    public WxChatMsgTypeEnum getMsgType() {
        return WxChatMsgTypeEnum.SUBSCRIBE;
    }

//    @Override
//    public String dealMsg(Map<String, String> messageMap) {
//        log.info("触发关注");
//        String fromUserName = messageMap.get("FromUserName");
//        String toUserName = messageMap.get("ToUserName");
//        String subscriContent="输入，验证码，获取登录码";
//        String msgType = messageMap.get("MsgType");
//
//        String createTimeStr = messageMap.get("CreateTime");
//        Long createTime = null;
//        if (createTimeStr != null && !createTimeStr.isEmpty()) {
//            try {
//                createTime = Long.valueOf(createTimeStr);
//            } catch (NumberFormatException e) {
//                log.error("Invalid CreateTime value: {}", createTimeStr, e);
//                // 可以选择使用当前时间作为默认值
//                createTime = System.currentTimeMillis() / 1000;
//            }
//        } else {
//            log.warn("CreateTime is null or empty, using current time.");
//            createTime = System.currentTimeMillis() / 1000;
//        }
//        String msgIdStr = messageMap.get("MsgId");
//        Long msgId = null;
//        if (msgIdStr != null && !msgIdStr.isEmpty()) {
//            try {
//                msgId = Long.valueOf(msgIdStr);
//            } catch (NumberFormatException e) {
//                log.error("Invalid MsgId value: {}", msgIdStr, e);
//                // 可以选择使用默认值或其他处理方式
//                msgId = System.currentTimeMillis();
//            }
//        } else {
//            log.warn("MsgId is null or empty, using current time.");
//            msgId = System.currentTimeMillis();
//        }
//
//        return
//        "<xml>\n" +
//                "  <ToUserName><![CDATA["+fromUserName+"]]>" + "</ToUserName>\n" +
//                "  <FromUserName><![CDATA["+toUserName+"]]></FromUserName>\n" +
//                "  <CreateTime>"+createTime+"</CreateTime>\n" +
//                "  <MsgType><![CDATA["+msgType+"]]></MsgType>\n" +
//                "  <Content><![CDATA["+subscriContent+"]]></Content>\n" +
//                "  <MsgId><![CDATA["+msgId+"]]></MsgId>\n "+
//                "</xml>";
//    }


    @Override
    public String dealMsg(Map<String, String> messageMap) {
        log.info("触发用户关注事件！");
        String fromUserName = messageMap.get("FromUserName");
        String toUserName = messageMap.get("ToUserName");
        String subscribeContent = "感谢您的关注，我是鸡翅！欢迎来学习从0到1社区项目";
        String content = "<xml>\n" +
                "  <ToUserName><![CDATA[" + fromUserName + "]]></ToUserName>\n" +
                "  <FromUserName><![CDATA[" + toUserName + "]]></FromUserName>\n" +
                "  <CreateTime>12345678</CreateTime>\n" +
                "  <MsgType><![CDATA[text]]></MsgType>\n" +
                "  <Content><![CDATA[" + subscribeContent + "]]></Content>\n" +
                "</xml>";
        return content;
    }
}
